# -*- coding: utf-8 -*-

"""
Description:
"""
import requests
from bs4 import BeautifulSoup
from utils.req import get_request

headers = {
    "sec-ch-ua-platform": '"macOS"',
    "Referer": "https://www.gbo-cn.com/zh-cn/",
    "X-Requested-With": "XMLHttpRequest",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
    "Accept": "*/*",
    "sec-ch-ua": '"Not;A=Brand";v="99", "Google Chrome";v="139", "Chromium";v="139"',
    "sec-ch-ua-mobile": "?0",
}


def search_product(keyword: str):
    url = "https://www.gbo-cn.com/zh-cn/"
    params = {"type": "351922129", "q": keyword}
    response = get_request(url=url, headers=headers, params=params)

    # 假设html是包含目标DOM的网页内容
    soup = BeautifulSoup(response.text, "html.parser")

    target_a = soup.find("a", class_="link-reset d-block p-1 p-sm-2 border-top")
    if target_a:
        return target_a.get("href")
    return None


def get_product_detail(url: str):
    html_str = """
    <style>        * {            margin: 0;            padding: 0;            box-sizing: border-box;        }        body {            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;            line-height: 1.6;            color: #2c3e50;            background-color: #f8f9fa;            padding: 20px 0;        }        .mb-2 {            margin-bottom: 2rem;            max-width: 800px;            margin: 0 auto;            padding: 0 20px;        }        .rollout {            background: white;            border-radius: 8px;            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);            margin-bottom: 16px;            overflow: hidden;            border: 1px solid #e9ecef;            transition: all 0.3s ease;        }        .rollout:hover {            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);            transform: translateY(-1px);        }        .rollout-trigger {            display: block;            padding: 18px 24px;            background-color: #fff;            color: #495057;            text-decoration: none;            font-weight: 600;            font-size: 1.05rem;            border-bottom: 2px solid #e9ecef;            position: relative;            transition: all 0.3s ease;            cursor: pointer;        }        .rollout-trigger:hover {            background-color: #f8f9fa;            color: #343a40;            text-decoration: none;        }        .rollout-trigger::before {            content: '';            position: absolute;            left: 0;            top: 0;            bottom: 0;            width: 4px;            background-color: #6c757d;            transition: all 0.3s ease;        }        .rollout-trigger:hover::before {            background-color: #495057;        }        .rollout-trigger::after {            content: '▼';            position: absolute;            right: 24px;            top: 50%;            transform: translateY(-50%);            transition: transform 0.3s ease;            font-size: 0.8rem;            color: #6c757d;        }        .rollout-trigger[aria-expanded="true"]::after {            transform: translateY(-50%) rotate(180deg);        }                #product-basic .rollout-trigger::before {            background-color: #007bff;        }        #product-basic .rollout-trigger:hover::before {            background-color: #0056b3;        }        #product-packing .rollout-trigger::before {            background-color: #28a745;        }        #product-packing .rollout-trigger:hover::before {            background-color: #1e7e34;        }        #product-detail .rollout-trigger::before {            background-color: #ffc107;        }        #product-detail .rollout-trigger:hover::before {            background-color: #e0a800;        }        .rollout-content {            background: white;        }        .rollout-content.show {            display: block;        }        .collapse {            display: none;        }        .collapse.show {            display: block;        }        .overflow-hidden {            overflow: hidden;        }        .table-responsive {            overflow-x: auto;        }        .table {            width: 100%;            margin-bottom: 0;            border-collapse: collapse;        }        .table.table-striped.base-data {            background: white;        }        .table tbody tr {            border-bottom: 1px solid #f1f3f4;            transition: background-color 0.2s ease;        }        .table tbody tr:hover {            background-color: #f8f9fa;        }        .table tbody tr:nth-child(even) {            background-color: #fafbfc;        }        .table tbody tr:nth-child(even):hover {            background-color: #f1f3f4;        }        .table tbody tr:last-child {            border-bottom: none;        }        .table th {            padding: 16px 24px;            font-weight: 600;            color: #495057;            background-color: #f8f9fa;            border: none;            font-size: 0.9rem;            width: 30%;            vertical-align: middle;            text-align: left;        }        .table td {            padding: 16px 24px;            color: #212529;            border: none;            font-size: 0.9rem;            vertical-align: middle;            line-height: 1.5;        }                .table td:matches([0-9]) {            font-weight: 500;            color: #495057;        }                .table tbody tr:first-child td {            line-height: 1.6;            color: #343a40;        }                .table td:contains("kg"),        .table td:contains("mm"),        .table td:contains("ml") {            font-family: 'Courier New', monospace;        }                @media (max-width: 768px) {            body {                padding: 10px 0;            }            .mb-2 {                padding: 0 15px;            }            .rollout-trigger {                padding: 15px 20px;                font-size: 1rem;            }            .rollout-trigger::after {                right: 20px;            }            .table th,            .table td {                padding: 12px 20px;                font-size: 0.85rem;            }            .table th {                width: 35%;            }        }        @media (max-width: 576px) {            .mb-2 {                padding: 0 10px;            }            .rollout-trigger {                padding: 12px 16px;                font-size: 0.95rem;            }            .rollout-trigger::after {                right: 16px;            }            .table th,            .table td {                padding: 10px 16px;                font-size: 0.8rem;            }            .table th {                width: 40%;            }                        .table-responsive {                -webkit-overflow-scrolling: touch;            }        }                .rollout {            animation: fadeInUp 0.5s ease-out;        }        @keyframes fadeInUp {            from {                opacity: 0;                transform: translateY(20px);            }            to {                opacity: 1;                transform: translateY(0);            }        }                #product-basic {            animation-delay: 0.1s;        }        #product-packing {            animation-delay: 0.2s;        }        #product-detail {            animation-delay: 0.3s;        }                .table tbody tr {            opacity: 0;            animation: fadeInRow 0.6s ease-out forwards;        }        @keyframes fadeInRow {            from {                opacity: 0;                transform: translateX(-10px);            }            to {                opacity: 1;                transform: translateX(0);            }        }        .table tbody tr:nth-child(1) { animation-delay: 0.1s; }        .table tbody tr:nth-child(2) { animation-delay: 0.2s; }        .table tbody tr:nth-child(3) { animation-delay: 0.3s; }        .table tbody tr:nth-child(4) { animation-delay: 0.4s; }        .table tbody tr:nth-child(5) { animation-delay: 0.5s; }                .table th {            letter-spacing: 0.5px;        }        .table td {            word-break: break-word;        }                .rollout:first-child {            margin-top: 20px;        }                .rollout-trigger:focus {            outline: 2px solid #007bff;            outline-offset: 2px;        }</style>
    """
    response = get_request(url=url, headers=headers)
    soup = BeautifulSoup(response.text, "html.parser")
    desc_dom = soup.find("div", attrs={"id": "product-basic"})
    if desc_dom:
        container = desc_dom.parent
        if container:
            return html_str + str(container)
    return None


def process_data(pro_code: str):
    pro_url = search_product(pro_code)
    print(pro_url)
    if pro_url:
        res = get_product_detail(pro_url)
        return res
    return None


if __name__ == "__main__":
    pro_url = search_product("123278")
    print(pro_url)
    if pro_url:
        res = get_product_detail(pro_url)
        with open("greniner_detail.html", "w", encoding="utf-8") as f:
            f.write(res)
